// pages/index/free/free_design.js
//import radom from '../../../utils/radom.js'
var app = getApp()  
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'https://img.xtuan.com/xcximgtest/images/free/freediser/fre_de4.jpg',
      'https://img.xtuan.com/xcximgtest/images/free/freediser/fre_de5.jpg',
      'https://img.xtuan.com/xcximgtest/images/free/freediser/fre_de6.jpg'
    ],
    city: '',
    cityid: '',
    proviceid: '',
    landing_page:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.viewdesign()


  },
  viewdesign:function(e){
    var that =this
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=view_design'),
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
        //'content-type': 'application/json;charset=utf-8'
      },
      success: function (res) {
        console.log(res.data)
        var landing_page = res.data.datas.landing_page;
        var bind_count = String(res.data.datas.bid_count);
        var numbs = String(res.data.datas.remain_count)
        //console.log(bind_count)
        //bind_count.toString()
        var s1 = numbs.substring(0, 1);//取前半部分
        var s2 = numbs.substring(1, 2);
        var s3 = numbs.substring(2, 3);
        var bids = bind_count.replace(/\b(0+)/gi, "")
        that.setData({
          landing_page: landing_page,
          num1: s1,
          num2: s2,
          num3: s3,
          count:bids
        })

      },
      fail:function(){
        console.log('请求失败!')
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var animatione = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    this.animation = animatione
    var next = true;
    //连续动画关键步骤
    setInterval(function () {
      if (next) {
        this.animation.scale(0.95).step()
        next = !next;
      } else {
        this.animation.scale(1).step()
        next = !next;
      }
      this.setData({
        animationDatas: animatione.export()
      })
    }.bind(this), 500)

  },

  //城市选择
  choosecity: function () {
    wx.navigateTo({
      url: '../public/custom/cucity'
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //form表单跳转提交
  formSubmit: function (e) {
    var that = this
    var city = e.detail.value.city
    var aleaname = e.detail.value.rqname
    var area = e.detail.value.area
    var tele = e.detail.value.tele
    var id = 2
    var title = '申请资料提交成功'
    var tsmall = '恭喜您，申请资料成功，我们将尽快与您联系!'
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var cityid = that.data.cityid
    var proviceid = that.data.proviceid
    var city = that.data.city
    var landing_page = that.data.landing_page

    if (city == '') {
      that.setData({
        alertmess: true,
        message: '请选择城市！'
      })
      that.settime()
      return false;
    } else if (aleaname == '') {
      that.setData({
        alertmess: true,
        message: '请填写小区名称！'
      })
      that.settime()
    } else if (area == '') {
      that.setData({
        alertmess: true,
        message: '请填写房屋面积！'
      })
      that.settime()
    } else if (tele == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
      return false;
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误！'
      })
      that.settime()
    }else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_design'),
        data: {
          phone: tele,
          address: aleaname,
          acreage: area,
          province: proviceid,
          city: cityid,
          landing_page: landing_page
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx: wx.navigateTo({
              url: 'applysuccess?data=' + [id, title, tsmall],
            })
          }else if(code==400){
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }
        }
      })
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
  skipTop:function(e){
    wx.pageScrollTo({
      scrollTop: 0
    })
  }
 
})